package com.tianyang.yukuaifu.business.login.model;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifu.business.login.bean.LoginOutput;
import com.tianyang.yukuaifu.business.main.contract.MainConfig;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.rxservice.client.PlateAgentClient;
import com.tianyang.yukuaifu.rxservice.client.PlateUserClient;

import java.util.HashMap;
import java.util.Map;

import rx.Observable;

/**
 * Description：登录模块model
 */

public class LoginModel {

    private static volatile LoginModel mInstance;

    private LoginModel() {
        if (mInstance != null) {
            throw new IllegalStateException("Already initialized.");
        }
    }

    public static LoginModel getInstance() {
        LoginModel result = mInstance;
        if (result == null) {
            synchronized (LoginModel.class) {
                result = mInstance;
                if (result == null) {
                    mInstance = result = new LoginModel();
                }
            }
        }
        return result;
    }

    /**
     * 登录接口
     *
     * @param phone 用户名
     * @param password    密码
     * @return
     */
    public Observable<LoginOutput> login(String phone, String password) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("username", phone);
        reqData.put("password", password);
        return PlateAgentClient.getInstance().merchantLogin(reqData);
    }

    /**
     * 验证码登录接口
     *
     * @return
     */
    public Observable<LoginOutput> smsLogin(String phone, String smsCode) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("phone", phone);
        reqData.put("verifyCode", smsCode);
        return PlateAgentClient.getInstance().merchantMobileLogin(reqData);
    }

    /**
     * 注册
     */
    public Observable<LoginOutput> register(String phone, String inviteCode, String posChannel) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("phone", phone);
        reqData.put("inviteCode", inviteCode);
        reqData.put("channel", posChannel);
        return PlateAgentClient.getInstance().register(reqData);
    }

    /**
     * 注册
     */
    public Observable<LoginOutput> register(String phone, String smsCode,  String password, String shareCode) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("phone", phone);
        reqData.put("verifyCode", smsCode);
        reqData.put("password", password);
        reqData.put("inviteCode", shareCode);
        return PlateAgentClient.getInstance().register(reqData);
    }

    /**
     * 发送验证码
     *
     * @return
     */
    public Observable<JSONObject> sendSms(String phone, String msgType) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("phone", phone);
        reqData.put("type", msgType);
        return PlateAgentClient.getInstance().sendSms(reqData);
    }

    /**
     * 忘记登录密码
     *
     * @param phone 账号
     * @param password 密码
     * @param smsCode  验证码
     * @return
     */
    public Observable<JSONObject> forgotLoginPwd(String phone, String password, String smsCode) {
        Map<String, String> reqData = new HashMap<>();
        reqData.put("phone", phone);
        reqData.put("password", password);
        reqData.put("authCode", smsCode);
        return PlateAgentClient.getInstance().forgetPassword(reqData);
    }

    /**
     * 修改密码
     */
    public Observable<JSONObject> modifyPassword(String phone, String password, String smsCode) {
        String identity = CacheManage.getInstance().getCache(CacheModel.IDENTITY, MainConfig.IDENTITY_MERCHANT);
        Map<String, String> reqData = new HashMap<>();
        if (TextUtils.equals(MainConfig.IDENTITY_MERCHANT, identity)) {
            reqData.put("phone", phone);
            reqData.put("authCode", smsCode);
            reqData.put("password", password);
            reqData.put("newPassword", password);
            reqData.put("repassword", password);
            return PlateUserClient.getInstance().updatePassword(reqData);
        } else {
            reqData.put("phone", phone);
            reqData.put("verifyCode", smsCode);
            reqData.put("newPassword", password);
            reqData.put("repassword", password);
            return PlateAgentClient.getInstance().modifyPassword(reqData);
        }
    }
}
